//
//  SetTradePswViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/6/22.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "SetTradePswViewController.h"
#import "KJSRequest.h"
#import "AppDelegate.h"
#import "ReSetTradePswViewController.h"
#import "CommonUtil.h"
@interface SetTradePswViewController ()
@property (nonatomic,weak) IBOutlet UIButton *submit;
@property (nonatomic,weak) IBOutlet UITextField *oldPswTF;
@property (nonatomic,weak) IBOutlet UITextField *pswTF;
@property (nonatomic,weak) IBOutlet UITextField *confirmPswTf;
@property (nonatomic,weak) IBOutlet UIButton *forgetPswBtn;

@property (weak, nonatomic) IBOutlet UIButton *oldPswEyeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmPswEyeBtn;
@property (weak, nonatomic) IBOutlet UIButton *pswNewEyeBtn;

@property (strong,nonatomic) NSString *tempStr;
@end

@implementation SetTradePswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"设置交易密码"];
    self.submit.layer.cornerRadius = 10;
    [self.submit addTarget:self action:@selector(modifiyTradePsw) forControlEvents:UIControlEventTouchUpInside];
    [self.forgetPswBtn addTarget:self action:@selector(forgetPsw) forControlEvents:UIControlEventTouchUpInside];
    
    self.oldPswTF.secureTextEntry = YES;
    self.pswTF.secureTextEntry = YES;
    self.confirmPswTf.secureTextEntry = YES;
    
    self.oldPswEyeBtn.tag = 1;
    [self.oldPswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    self.confirmPswEyeBtn.tag = 2;
    [self.confirmPswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    self.pswNewEyeBtn.tag = 3;
    [self.pswNewEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - custom
-(void)swichPasswordVisiable:(id)sender
{
    UIButton *eyeBtn = (UIButton *)sender;
    switch (eyeBtn.tag) {
        case 1:
        {
            self.tempStr = self.oldPswTF.text;
            self.oldPswTF.text = @"";
            self.oldPswTF.secureTextEntry = !self.oldPswTF.secureTextEntry;
            self.oldPswTF.text = self.tempStr;
        }
            break;
        case 2:
        {
            self.tempStr = self.confirmPswTf.text;
            self.confirmPswTf.text = @"";
            self.confirmPswTf.secureTextEntry = !self.confirmPswTf.secureTextEntry;
            self.confirmPswTf.text = self.tempStr;
        }
            break;
        case 3:
        {
            self.tempStr = self.pswTF.text;
            self.pswTF.text = @"";
            self.pswTF.secureTextEntry = !self.pswTF.secureTextEntry;
            self.pswTF.text = self.tempStr;
        }
            break;
        default:
            break;
    }
    
}
-(void)modifiyTradePsw
{
    if([self.oldPswTF.text isEqualToString:@""]){
        [self showAlertViewWithMsg:@"请输入旧密码"];
    }else if ([self.pswTF.text isEqualToString:@""]){
        [self showAlertViewWithMsg:@"请输入新密码"];
    }else if (![CommonUtil validateTradePsw:self.pswTF.text]){
        [GUATool showDialog:@"请输入6-16位交易密码" message:nil waitingTime:1.5];
    }else if (![self.pswTF.text isEqualToString:self.confirmPswTf.text]) {
        [GUATool showDialog:@"输入的两次新密码不同" message:nil waitingTime:1.5];
    }else if ([self.pswTF.text isEqualToString:self.oldPswTF.text]){
        [self showAlertViewWithMsg:@"新旧密码不能一样"];
    }else if (CurentDelegate.account.partyId) {
        [KJSRequest UpdateTradePasswordWithTradePwd:self.pswTF.text oldTradePwd:self.oldPswTF.text partyId:CurentDelegate.account.partyId ret:^(BasicResult *model) { 
            if ([model.returnMsg isEqualToString:@"success"]) {
                [GUATool showDialog:@"交易密码修改成功" message:nil waitingTime:1.5];
                [self dismiss:YES];
            }
        }];
    }else{
        [self showAlertViewWithMsg:@"请登录"];
    }
}
-(void)forgetPsw
{
    ReSetTradePswViewController *reSetTradePswVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ReSetTradePswViewController"];
    reSetTradePswVC.resetSuccess = ^(void){
        NSLog(@"回调了");
        [self dismiss:YES];
    };
    [self displayViewController:reSetTradePswVC];
}
-(void)showAlertViewWithMsg:(NSString *)msg
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

@end
